const app = getApp()
const db = wx.cloud.database()
const _ = db.command
const $ = db.command.aggregate

let floor = null

Page({
  data: {
    date: '请点击选择'
  },
  onLoad(opt) {
    floor = opt.floor ? opt.floor : null

    if (floor) {
      wx.setNavigationBarTitle({
        title: floor + '可预定的区域',
      })
    }

  },
  onShow() {
    this.setData({
      date: app._getNianYuiRi()
    }, success => {
      this.getList()
    })
  },
  getList() {
    if (this.data.date == '请点击选择') {
      wx.showToast({
        icon: 'error',
        title: '请选择日期',
      })
      return
    }
    db.collection('venue').aggregate()
      .match({
        floor: floor,
        timeList: _.not(_.eq(this.data.date))
      })
      .group({
        _id: '$region',
        num: $.sum(1)
      })
      .sort({
        _id: 1
      })
      .end()
      .then(res => {
        console.log(floor + '可预定教室列表', res)
        this.setData({
          list: res.list
        })
      })

  },
  // 选择日期
  bindDateChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let _this = this
    _this.setData({
      date: e.detail.value
    }, success => {
      if (app._getNianYuiRi() <= _this.data.date) {
        _this.getList()
      } else {
        _this.setData({
          list: []
        })
        wx.showModal({
          title: "提示",
          content: "只可以选择今日或者以后的日期，之前的日期没法选择",
          showCancel: false
        })
      }

    })
  },
  //去单个教室预约页
  goDetail(e) {
    let item = e.currentTarget.dataset.item
    if (floor) {
      wx.navigateTo({
        url: '/pages/reserveDetail/reserveDetail?region=' + item._id + "&date=" + this.data.date + "&floor=" + floor,
      })
    }

  },
})